class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();

        vector<int> f(n + 1, 1);
        auto g = f;
        int ret = -0x3f3f3f3f;

        for (int i = 1; i < n + 1; i++)
        {
            f[i] = max(nums[i - 1], max(g[i - 1] * nums[i - 1], f[i - 1] * nums[i - 1]));
            g[i] = min(nums[i - 1], min(g[i - 1] * nums[i - 1], f[i - 1] * nums[i - 1]));

            ret = max(ret, f[i]);
        }

        return ret;
    }
};